let PINDAO = 0;

enum PIN_Motor {
    //% block="M1"
    5,
    //% block="M2"
    7
}
enum PIN_M_PN {
    //% block="正转"
    HIGH,
    //% block="反转"
    LOW
}

enum PIN_SERVO {
    //% block="A0"
    A0,
    //% block="A1"
    A1
}

enum Irr_ZHUANGTAI {
    //% block="上"
    241,
    //% block="下"
    224,
    //% block="左"
    192,
    //% block="右"
    208,
    //% block="F1"
    238,
    //% block="F2"
    207,
    //% block="F3"
    246,
    //% block="F4"
    215,
    //% block="F5"
    231,
    //% block="F6"
    198
}

enum PIN_IRRECV {
    //% block="频道1"
    1,
    //% block="频道2"
    2,
    //% block="频道3"
    3,
    //% block="频道4"
    4,
    //% block="频道5"
    5,
    //% block="频道6"
    6,
    //% block="频道7"
    7,
    //% block="频道8"
    8
}


enum KAIGUAN {
    //% block="高"
    HIGH,
    //% block="低"
    LOW
}

enum GAODI {
    //% block="开"
    LOW,
    //% block="关"
    HIGH
}

enum LINGMIE {
    //% block="亮"
    LOW,
    //% block="灭"
    HIGH
}
enum LINGMIE1 {
    //% block="亮"
    LOW,
    //% block="灭"
    HIGH
}
enum LINGMIE2 {
    //% block="亮"
    LOW,
    //% block="灭"
    HIGH
}
enum LINGMIE3 {
    //% block="亮"
    LOW,
    //% block="灭"
    HIGH
}
enum PIN_AnalogWrite {
    //% block="A0"
    A0,
    //% block="A1"
    A1,
    //% block="A2"
    A2,
    //% block="A3"
    A3,
    //% block="A4"
    A4,
    //% block="A5"
    A5
}

enum PIN_DigitalWrite {
    //% block="D2"
    2,
    //% block="D3"
    3,
    //% block="D4"
    4,
    //% block="D9"
    9,
    //% block="D10"
    10,
    //% block="D11"
    11,
    //% block="D12"
    12,
    //% block="D13"
    13,
    //% block="A0"
    A0,
    //% block="A1"
    A1,
    //% block="A2/超声波"
    A2,
    //% block="A3"
    A3,
    //% block="A4"
    A4,
    //% block="A5/LCD/数码管"
    A5
}
enum PIN_DigitalWrite2 {
    //% block="A0"
    A0,
    //% block="A1"
    A1,
}




//% color="#00a8f3" iconWidth=50 iconHeight=40
namespace letongMotor {

    //% block="控制[YINJIAO]引脚的电平状态[KAIGUAN]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_DigitalWrite" 
    //% KAIGUAN.shadow="dropdown" KAIGUAN.options="KAIGUAN" KAIGUAN.defl="HIGH"
    export function kaiguan(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let KAIGUAN = parameter.KAIGUAN.code;

        Generator.addSetup(`kaiguan_setup`, `pinMode(${YINJIAO}, OUTPUT);`);
        Generator.addCode(`digitalWrite(${YINJIAO},${KAIGUAN});`);
    }

    //% block="控制[YINJIAO]引脚的模拟量为[LIANGDU]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_AnalogWrite" 
    //% LIANGDU.shadow="range"   LIANGDU.params.min=0    LIANGDU.params.max=255    LIANGDU.defl=255
    export function liangdu(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let LIANGDU = parameter.LIANGDU.code;

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addObject(`OxpontIRremotePwm_object`, `OxpontIRremotePwm`, `irrecv;`);
        Generator.addSetup(`enableTimer2_setup`, `irrecv.enableTimer2();`);

        Generator.addCode(`irrecv.pwmWrite(${YINJIAO},${LIANGDU});`);
    }

    //% block="控制[YINJIAO]马达的方向[FANGXIANG]速度为[LIANGDU]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_Motor" 
    //% FANGXIANG.shadow="dropdown" FANGXIANG.options="PIN_M_PN"
    //% LIANGDU.shadow="range"   LIANGDU.params.min=0    LIANGDU.params.max=255    LIANGDU.defl=255
    export function motor(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let LIANGDU = parameter.LIANGDU.code;
        let FANGXIANG = parameter.FANGXIANG.code;
        let m_pin = 8;
        let m_fx = '255-';
        if (YINJIAO === '7') {
            m_pin = 8;
        } else {
            m_pin = 6;
        }

        if (FANGXIANG === 'HIGH') {
            m_fx = '255-';
        } else {
            m_fx = '0+';
        }
        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wireeeee', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');


        Generator.addObject(`OxpontIRremotePwm_object`, `OxpontIRremotePwm`, `irrecv;`);

        Generator.addSetup(`enableTimer2_setup`, `irrecv.enableTimer2();`);
        Generator.addSetup(`moter_setup`, `pinMode(${m_pin},OUTPUT);`);

        Generator.addCode(
            `digitalWrite(${m_pin}, ${FANGXIANG}); \n\  irrecv.pwmWrite(${YINJIAO},${m_fx}${LIANGDU});`);
    }

    //% block="LED[YINJIAO]状态为[LINGMIE]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_DigitalWrite" 
    //% LINGMIE.shadow="dropdown" LINGMIE.options="LINGMIE" LINGMIE.defl="HIGH"
    export function LEDlm(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let LINGMIE = parameter.LINGMIE.code;

        Generator.addSetup(`LEDlm_setup`, `pinMode(${YINJIAO}, OUTPUT);`);
        Generator.addCode(`digitalWrite(${YINJIAO},${LINGMIE});`);

    }

    //% block="LED[YINJIAO]的亮度为[LIANGDU]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_AnalogWrite" 
    //% LIANGDU.shadow="range"   LIANGDU.params.min=0    LIANGDU.params.max=255    LIANGDU.defl=255
    export function LEDliangdu(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let LIANGDU = parameter.LIANGDU.code;
        Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        // Generator.addInclude('MPython', '#include <Arduino.h>\n\#include       <Wire.h> \n\#include <SoftwareSerial.h>\n\#include <OxpontIRremotePwm.h>');

        Generator.addObject(`OxpontIRremotePwm_object`, `OxpontIRremotePwm`, `irrecv;`);

        Generator.addSetup(`enableTimer2_setup`, `irrecv.enableTimer2();`);

        Generator.addCode(`irrecv.pwmWrite(${YINJIAO},255-${LIANGDU});`);
    }
    //% block="三色灯设置：红色[LINGMIE1] 绿色[LINGMIE2] 黄色[LINGMIE3]" blockType="command"
    //% LINGMIE1.shadow="dropdown" LINGMIE1.options="LINGMIE1" LINGMIE1.defl="HIGH"
    //% LINGMIE2.shadow="dropdown" LINGMIE2.options="LINGMIE1" LINGMIE2.defl="HIGH"
    //% LINGMIE3.shadow="dropdown" LINGMIE3.options="LINGMIE1" LINGMIE3.defl="HIGH"


    export function TreeLED(parameter: any, block: any) {
        let LED_R = parameter.LINGMIE1.code;
        let LED_G = parameter.LINGMIE2.code;
        let LED_B = parameter.LINGMIE3.code;

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addObject(`OxpontIRremotePwm_object`, `OxpontIRremotePwm`, `irrecv;`);

        Generator.addSetup(`enableTimer2_setup`, `irrecv.enableTimer2();`);


        Generator.addSetup(`TreeLED_setup`, `pinMode(2, OUTPUT);\n\  pinMode(3,OUTPUT);\n\  pinMode(4,OUTPUT);`);
        Generator.addCode(`digitalWrite(4, ${LED_R}); \n\  digitalWrite(3, ${LED_G}); \n\ digitalWrite(2, ${LED_B});`);

    }
    //% block="按键 [BUTTON] 被按下？" blockType="boolean"
    //% Flag.shadow="boolean"
    //% BUTTON.shadow="dropdown" BUTTON.options="PIN_DigitalWrite" 
    export function buttonIsPressed(parameter: any, block: any) {
        let button = parameter.BUTTON.code;
        let code;

        code = `1-digitalRead(${button})`;
        Generator.addSetup(`buttonIsPressed_setup`, `pinMode(${button}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }
    //% block="倾斜传感器 [QINGXIE] 倾斜？" blockType="boolean"
    //% Flag.shadow="boolean"
    //% QINGXIE.shadow="dropdown" QINGXIE.options="PIN_DigitalWrite" 
    export function Qingxie(parameter: any, block: any) {
        let qingxie = parameter.QINGXIE.code;
        let code;

        code = `1-digitalRead(${qingxie})`;

        Generator.addSetup(`Qingxie_setup`, `pinMode(${qingxie}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }
    //% block="人体传感器 [RENTI] 检测到有人？" blockType="boolean"
    //% Flag.shadow="boolean"
    //% RENTI.shadow="dropdown" RENTI.options="PIN_DigitalWrite" 
    export function Renti(parameter: any, block: any) {
        let renti = parameter.RENTI.code;
        let code;

        code = `digitalRead(${renti})`;

        Generator.addSetup(`Renti_setup`, `pinMode(${renti}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }

    //% block="红外传感器 [AN] 检测到物体？" blockType="boolean"
    //% Flag.shadow="boolean"
    //% AN.shadow="dropdown" AN.options="PIN_DigitalWrite2" 
    export function An(parameter: any, block: any) {
        let an = parameter.AN.code;
        let code;

        code = `digitalRead(${an})`;

        Generator.addSetup(`An_setup`, `pinMode(${an}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }
    //% block="雨滴传感器 [YUDI] 检测到下雨？" blockType="boolean"
    //% Flag.shadow="boolean"
    //% YUDI.shadow="dropdown" YUDI.options="PIN_DigitalWrite" 
    export function Yudi(parameter: any, block: any) {
        let yudi = parameter.YUDI.code;
        let code;

        code = `digitalRead(${yudi})`;

        Generator.addSetup(`Yudi_setup`, `pinMode(${yudi}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }
    //% block="亮度传感器 [LIANG] 数值" blockType="reporter"
    //% Flag.shadow="boolean"
    //% LIANG.shadow="dropdown" LIANG.options="PIN_AnalogWrite" 
    export function Liang(parameter: any, block: any) {
        let liang = parameter.LIANG.code;
        let code;

        code = `analogRead(${liang})`;

        Generator.addSetup(`Liang_setup`, `pinMode(${liang}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }
    //% block="声音传感器 [SHENG] 数值" blockType="reporter"
    //% Flag.shadow="boolean"
    //% SHENG.shadow="dropdown" SHENG.options="PIN_AnalogWrite" 
    export function Sheng(parameter: any, block: any) {
        let sheng = parameter.SHENG.code;
        let code;

        code = `analogRead(${sheng})`;

        Generator.addSetup(`Sheng_setup`, `pinMode(${sheng}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }
    //% block="旋钮传感器 [XUAN] 数值" blockType="reporter"
    //% Flag.shadow="boolean"
    //% XUAN.shadow="dropdown" XUAN.options="PIN_AnalogWrite" 
    export function Xuan(parameter: any, block: any) {
        let xuan = parameter.XUAN.code;
        let code;

        code = `analogRead(${xuan})`;

        Generator.addSetup(`Xuan_setup`, `pinMode(${xuan}, INPUT);`);

        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);

    }
    //% block="遥控器设置频道[PINDAO]，接口为[YINJIAO]" blockType="command"
    //% PINDAO.shadow="dropdown" PINDAO.options="PIN_IRRECV" 
    //% YINJIAO.shadow="dropdown"  YINJIAO.options="PIN_DigitalWrite"
    export function Irrecv(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        PINDAO = parameter.PINDAO.code;

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addObject(`OxpontIRremotePwm_object`, `OxpontIRremotePwm`, `irrecv;`);

        Generator.addSetup(`enableTimer2_setup`, `irrecv.enableTimer2();`);
        Generator.addSetup(`Irrecv_setup`, `irrecv.enableIRIn(${YINJIAO});`);
    }

    //% block="遥控器接收 [ZHUANGTAI] " blockType="boolean"
    //% Flag.shadow="boolean"
    //% ZHUANGTAI.shadow="dropdown" ZHUANGTAI.options="Irr_ZHUANGTAI" 
    export function IrrIsPressed(parameter: any, block: any) {
        let ZHUANGTAI = parameter.ZHUANGTAI.code;

        Generator.addCode(`irrecv.getChannel() == ${PINDAO}  && irrecv.getKey() == ${ZHUANGTAI}`);
    }

    //% block="遥控器停止接收" blockType="command"
    export function IrrecvStop(parameter: any, block: any) {
        Generator.addCode(`irrecv.disableIRIn();`);
    }

    //% block="蜂鸣器[YINJIAO]状态为[LINGMIE]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_DigitalWrite" 
    //% LINGMIE.shadow="dropdown" LINGMIE.options="GAODI" LINGMIE.defl="HIGH"
    export function BEEP(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let LINGMIE = parameter.LINGMIE.code;

        Generator.addSetup(`BEEP_setup`, `pinMode(${YINJIAO}, OUTPUT);`);
        Generator.addCode(`digitalWrite(${YINJIAO},${LINGMIE});`);

    }

    //% block="多彩灯设置：R[LED_R] G[LED_G] B[LED_B]" blockType="command"
    //% LED_R.shadow="range"   LED_R.params.min=0    LED_R.params.max=255    LED_R.defl=0
    //% LED_G.shadow="range"   LED_G.params.min=0    LED_G.params.max=255    LED_G.defl=0
    //% LED_B.shadow="range"   LED_B.params.min=0    LED_B.params.max=255    LED_B.defl=0
    export function RGBLED(parameter: any, block: any) {
        let LED_R = parameter.LED_R.code;
        let LED_G = parameter.LED_G.code;
        let LED_B = parameter.LED_B.code;

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addObject(`OxpontIRremotePwm_object`, `OxpontIRremotePwm`, `irrecv;`);

        Generator.addSetup(`enableTimer2_setup`, `irrecv.enableTimer2();`);

        Generator.addCode(
            `irrecv.pwmWrite(4, 255-${LED_R}); \n\  irrecv.pwmWrite(3, 255-${LED_G}); \n\  irrecv.pwmWrite(2, 255-${LED_B});`);

    }

    //% block="数码管显示：[STING] 亮度为：[LIANGDU]" blockType="command"
    //% STING.shadow= "string" STING.defl= "0"
    //% LIANGDU.shadow="range"   LIANGDU.params.min=1    LIANGDU.params.max=8    LIANGDU.defl= 5
    export function SHUMAGUAN(parameter: any, block: any) {
        let STING = parameter.STING.code;
        let LIANGDU = parameter.LIANGDU.code;

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addInclude('AIP650', '#include <AIP650.h>');

        Generator.addObject(`AIP650_setup`, `AIP650`, `aip650;`);
        Generator.addSetup(`SHUMAGUAN_setup`, `aip650.init(18,19);\n\  aip650.setShowLight(${LIANGDU});//设置亮度`);
        Generator.addCode(`aip650.displayString(${STING});`);

    }

    //% block="数码管清除全部显示" blockType="command"
    export function SHUMAGUAN_clear(parameter: any, block: any) {

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addInclude('AIP650', '#include <AIP650.h>');

        Generator.addObject(`AIP650_setup`, `AIP650`, `aip650;`);
        Generator.addCode(`aip650.clearDisplay();`);

    }

    //% block="舵机设置[GUANJIAO]角度为[JIAODU]" blockType="command"
    //% GUANJIAO.shadow="dropdown" GUANJIAO.options="PIN_SERVO" 
    //% JIAODU.shadow="angle" JIAODU.params.edge=1
    export function SERVO(parameter: any, block: any) {
        let JIAODU = parameter.JIAODU.code;
        let GUANJIAO = parameter.GUANJIAO.code;

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addInclude('Servo', '#include <Servo.h>');

        Generator.addObject(`Serv_object`, `Servo`, `servo_${GUANJIAO};`);

        Generator.addSetup(`Servo_setup`, `servo_${GUANJIAO}.attach(${GUANJIAO});`);

        Generator.addCode(`servo_${GUANJIAO}.write(${JIAODU});`);
    }

    //% block="超声波传感器距离(CM)" blockType="reporter"
    export function HCSR04(parameter: any, block: any) {

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addInclude('HCSR04', '#include <OxpontHCSR04.h>');
        Generator.addObject(`HCSR04_object`, `OxpontHCSR04`, `hcsr04;`);
        Generator.addCode(`hcsr04.getDistanceCM(16,17)`);
    }

    //% block="初始化LCD屏幕"
    export function LCD(parameter: any, block: any) {

        // Generator.addInclude('Arduino', '#include <Arduino.h>');
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('OxpontIRremotePwm', '#include <OxpontIRremotePwm.h>');

        Generator.addInclude('LiquidCrystal_I2C', '#include <LiquidCrystal_I2C.h>');
       
        Generator.addObject(`LCD_object`, `LiquidCrystal_I2C`, `lcd(0x27,16,2);`);
        Generator.addSetup(`LCD_setup`, `lcd.init();\n\  lcd.backlight();`);
    }

    //% block="设置LCD清除屏幕"
    export function LCD_clear(parameter: any, block: any) {

        Generator.addCode(`lcd.setCursor(0,0);\n\  lcd.clear();`);
    }

    //% block="设置LCD第[hang]行第[lie]列起显示为[dayin]"
    //% hang.shadow="range"   hang.params.min=1    hang.params.max=2    hang.defl=1
    //% lie.shadow="range"   lie.params.min=1    lie.params.max=16    lie.defl=1
    //% dayin.shadow= "string" dayin.defl= "Hello"
    export function LCD_print(parameter: any, block: any) {

        let hang = parameter.hang.code;
        let lie = parameter.lie.code;
        let dayin = parameter.dayin.code;

        Generator.addCode(`lcd.setCursor(${lie}-1,${hang}-1); \n\  lcd.print(${dayin});`);
    }

}

